package com.cn.auth.controller.census;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import java.io.FileWriter;
import java.io.IOException;

/**
 * describe: 测试样例
 * current user Maochao.zhu
 * current system 2020/1/3
 */
public class Example {

    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) {
        generatedPage();
    }

    /**
     * 生成html页面
     */
    public static   void generatedPage(){
        //构造模板引擎
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix("templates/");//模板所在目录，相对于当前classloader的classpath。
        resolver.setSuffix(".html");//模板文件后缀
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);

        //构造上下文(Model)
        Context context = new Context();
        context.setVariable("name", "蔬菜列表");
        context.setVariable("array", new String[]{"土豆", "番茄", "白菜", "芹菜","土豆", "番茄", "白菜", "芹菜"});
        //渲染模板
        FileWriter write = null;
        try {
            //生成页面的路径地址
            write = new FileWriter("D:/workspace_idea/springboot-auth-up/src/main/resources/templates/web/result.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        templateEngine.process("example", context, write);
    }
}
